﻿from direct.showbase.ShowBase import ShowBase
from panda3d.core import *

class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        
        # 加载立方体模型
        self.cube = self.loader.loadModel("models/box")
        self.cube.reparentTo(self.render)
        self.cube.setScale(2)
        self.cube.setPos(0, 0, 0)
        
        # 设置纹理
        texture = self.loader.loadTexture("textures/wood.jpg")
        self.cube.setTexture(texture, 1)
        
        # 添加旋转任务
        self.taskMgr.add(self.rotate_cube, "rotate_cube_task")
        
        # 设置相机位置
        self.camera.setPos(0, -10, 0)
        self.camera.lookAt(0, 0, 0)
        
        # 添加灯光
        ambient_light = AmbientLight("ambient_light")
        ambient_light.setColor((0.2, 0.2, 0.2, 1))
        self.render.setLight(self.render.attachNewNode(ambient_light))
        
        directional_light = DirectionalLight("directional_light")
        directional_light.setColor((0.8, 0.8, 0.8, 1))
        directional_light.setDirection((1, 1, -1))
        self.render.setLight(self.render.attachNewNode(directional_light))
    
    def rotate_cube(self, task):
        # 每帧旋转立方体
        dt = globalClock.getDt()
        self.cube.setH(self.cube.getH() + 60 * dt)
        return task.cont

app = MyApp()
app.run()